ARTICLES > Lotus notes

Printing on Lotus notes web agent Turn Back

2016-09-08 16:55:06

This is a technique used for printing documents on the browser for Lotusnotes Web application platform.
It uses javascript in through ajax function to run commands on lotusnotes script and return by print plain text with a Html header.
 
This may not be the best way. But it can be really easy.
 
Step 1:
Create a button for call the print function on your web page.
<button type="button" onclick="printVoucher()"> Print This Page </button>
 
Then press it. Run below function.
function createXMLHttpRequest() {
      if (window.ActiveXObject) {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      }
      else if (window.XMLHttpRequest) {
            xmlHttp = new XMLHttpRequest();                
      }
}
 
function printVoucher(){
      createXMLHttpRequest();            
      var url="server_path/printVoucher";   // Agent name     
      var param="?OpenAgent&unid=_DOCUMENT_ID_";
      param+="&rand="+Math.random(1);
      param=encodeURI(param);
      xmlHttp.open("GET", url+param, true);
      xmlHttp.onreadystatechange = callback_printVoucher;
      xmlHttp.send(null);
}
 
function callback_printVoucher() {
     // Setting popup window
     w=960;
     h=600;
     // Set popup as center screen
     LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
     TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
 
    if (xmlHttp.readyState == 4) {
           if (xmlHttp.status == 200) {
 
                  var myWindow=window.open('','windowPrint','left='+LeftPosition+',top='+TopPosition+',width='+w+',height='+h+',scrollbars=yes,resizable=1');
                  if (!myWindow.opener) {
                           myWindow.opener = self;
                           myWindow.focus();
                   }
                   myWindow.document.write(xmlHttp.responseText);
                   myWindow.document.close();
                   myWindow.print();
           }
      }
}

Step 2:
Create agent command script on Lotus notes

Option Public
Option Declare
 
Use "WEB Utility Library"
Use "Reimbursement Library"
Sub Initialize
Dim s As New NotesSession
Dim cdoc As NotesDocument
Set cdoc = s.Documentcontext
Dim form_sp,form_sp1 As Variant
 
'get QueryString
form_sp=Split(cdoc.Query_String_Decoded(0),"&")
 
'get document ID from path
form_sp1=Split(form_sp(1),"=")
 
 
Dim db As NotesDatabase
Set db = s.Currentdatabase
Dim v As NotesView
 
'Notes View document lists
Set v = db.Getview("DOCBYUNIVERSALID")
Dim doc As NotesDocument
Set doc = v.Getdocumentbykey(form_sp1(1))
Dim Itemdc As NotesDocumentCollection
Set Itemdc = GetItemListInDoc(doc)
 
Dim i As Integer
Dim ErrorText As String
 
Dim NName As NotesName
 
Dim allDetail() As ExpenseData
Dim j As Integer, k As Integer, m As Integer
Dim advanceno As String, payee As String, logo As string
Dim Total As Double,Vat As Double,WHT As Double,GrandTotal As Double
Dim edoc As NotesDocument
 
Set edoc = Itemdc.GetFirstDocument
i = 1
j = 0
Total = 0
Vat = 0 
WHT = 0 
Grandtotal = 0
 
Print "Content-Type:text/plain"
Print "Content-Type:text/html"
Print |<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 
<style tyle="text/css" media="all">
'Style sheet for all
</style>
 
<style tyle="text/css" media="print">
'Style sheet for printing
</style>
 
<!--[if lt IE 9]>
    <script src="http://html5shiv-printshiv.googlecode.com/svn/trunk/html5shiv-printshiv.js"></script>
<![endif]-->
|
 
 
Print |</head>|
Print |<body><div id="wrap">|
 
Print "<h1>Riembursement</center></h1><h2>Payment Request Voucher</h2>"
 
'Dim var As Variant
'var = Evaluate(|@Text(@Year(@TextToTime(|+CStr(doc.DocNoDate(0))+|)))|,doc)
Dim dt As New NotesDateTime(doc.DocNoDate(0))
 
 
Print |<table class="tbMain" border="1" width="100%"><thead>|
Print "<tr>"
Print |<th width="8%">Index</th>|
Print "<th>Partcular</th>"
print |<th width="10%">Tax Invoice</th>|
Print |<th width="10%">Currenty<br/>Total</th>|
Print |<th width="10%">Vat</th>|
Print |<th width="10%">Tax</th>|
Print |<th width="10%">Net Amount</th>|
Print "</tr></thead><tbody>"
 
 
Do While Not(edoc Is Nothing)
ReDim Preserve allDetail(j)
Print |<tr valign="top">|
Print "<td align=center>"+CStr(i)+"</td>"
Print "<td>"+CStr(edoc.Description(0))+"</td>"
Print |<td align=center>|+edoc.InvoiceNo(0)+|</td>|
Print "<td align=right>"+CStr(Format(edoc.Total(0), "#,#0.00"))+"</td>"
Print "<td align=right>"+CStr(Format(edoc.Vat(0), "#,#0.00"))+"</td>"
Print "<td align=right>"+CStr(Format(edoc.WHT(0), "#,#0.00"))+"</td>"
Print "<td align=right>"+CStr(Format(edoc.Grandtotal(0), "#,#0.00"))+"</td>"
Print "</tr>"
Total = Total + edoc.Total(0)
Vat = Vat + edoc.Vat(0)
WHT = WHT + edoc.WHT(0)
Grandtotal = Grandtotal + edoc.Grandtotal(0)
j = j+1
i = i+1
Set edoc = Itemdc.GetNextDocument(edoc)
Loop
 
Print "</tbody></table>"
 
Print |
<center class="no-print">
<button type="button" class="btn btn-success" onclick="window.print();">Print Report</button>
<button type="button" class="btn btn-danger" onClick="window.close()" >Close</button>
</center>
|
 
Print |</div></body></html>|
 
doc.IsPrinted = "1"
Call doc.save(True,True)
 
Exit Sub
 
ErrorHandler:
ErrorText = "Error On Initialize In Print error:" + Error() + " On Code Line = " & CStr(Erl)
Print ErrorText
Exit Sub
 
End Sub
 
Step 3:
Testing and Enjoy work
Turn Back